#!/bin/bash

# 抓黑枪游戏 API 测试脚本

echo "========================================="
echo "🎮 抓黑枪游戏 API 测试"
echo "========================================="
echo ""

BASE_URL="http://localhost:3000/api/game"

# 颜色输出
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${BLUE}1. 测试健康检查${NC}"
curl -s http://localhost:3000/health | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}2. 获取房间列表（应该为空）${NC}"
curl -s "$BASE_URL/room/list" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}3. 创建房间${NC}"
ROOM_RESPONSE=$(curl -s -X POST "$BASE_URL/room/create" \
  -H "Content-Type: application/json" \
  -H "user-id: user_alice" \
  -H "user-name: Alice" \
  -d '{"roomName": "Alice的房间", "maxPlayers": 3}')

echo "$ROOM_RESPONSE" | python3 -m json.tool
ROOM_ID=$(echo "$ROOM_RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['roomId'])")
echo -e "${GREEN}房间ID: $ROOM_ID${NC}"
echo -e "\n"

echo -e "${BLUE}4. 再次获取房间列表（应该有1个房间）${NC}"
curl -s "$BASE_URL/room/list" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}5. Bob 加入房间${NC}"
curl -s -X POST "$BASE_URL/room/join" \
  -H "Content-Type: application/json" \
  -H "user-id: user_bob" \
  -H "user-name: Bob" \
  -d "{\"roomId\": \"$ROOM_ID\"}" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}6. 获取房间详情${NC}"
curl -s "$BASE_URL/room/detail?roomId=$ROOM_ID" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}7. Bob 准备${NC}"
curl -s -X POST "$BASE_URL/room/ready" \
  -H "Content-Type: application/json" \
  -H "user-id: user_bob" \
  -H "user-name: Bob" \
  -d "{\"roomId\": \"$ROOM_ID\"}" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}8. Charlie 加入房间${NC}"
curl -s -X POST "$BASE_URL/room/join" \
  -H "Content-Type: application/json" \
  -H "user-id: user_charlie" \
  -H "user-name: Charlie" \
  -d "{\"roomId\": \"$ROOM_ID\"}" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}9. 再次查看房间（应该有3个玩家）${NC}"
curl -s "$BASE_URL/room/detail?roomId=$ROOM_ID" | python3 -m json.tool
echo -e "\n"

echo -e "${BLUE}10. 创建带密码的房间${NC}"
PASSWORD_ROOM=$(curl -s -X POST "$BASE_URL/room/create" \
  -H "Content-Type: application/json" \
  -H "user-id: user_dave" \
  -H "user-name: Dave" \
  -d '{"roomName": "Dave的私密房间", "password": "123456", "maxPlayers": 3}')

echo "$PASSWORD_ROOM" | python3 -m json.tool
PASSWORD_ROOM_ID=$(echo "$PASSWORD_ROOM" | python3 -c "import sys, json; print(json.load(sys.stdin)['data']['roomId'])" 2>/dev/null || echo "")
echo -e "\n"

if [ -n "$PASSWORD_ROOM_ID" ]; then
  echo -e "${BLUE}11. 尝试不带密码加入（应该失败）${NC}"
  curl -s -X POST "$BASE_URL/room/join" \
    -H "Content-Type: application/json" \
    -H "user-id: user_eve" \
    -H "user-name: Eve" \
    -d "{\"roomId\": \"$PASSWORD_ROOM_ID\"}" | python3 -m json.tool
  echo -e "\n"

  echo -e "${BLUE}12. 用正确密码加入${NC}"
  curl -s -X POST "$BASE_URL/room/join" \
    -H "Content-Type: application/json" \
    -H "user-id: user_eve" \
    -H "user-name: Eve" \
    -d "{\"roomId\": \"$PASSWORD_ROOM_ID\", \"password\": \"123456\"}" | python3 -m json.tool
  echo -e "\n"
fi

echo -e "${BLUE}13. 最终房间列表${NC}"
curl -s "$BASE_URL/room/list" | python3 -m json.tool
echo -e "\n"

echo -e "${GREEN}========================================="
echo "✅ 测试完成！"
echo "=========================================${NC}"
